package com.dl7.player.utils;

import ohos.app.Context;
import ohos.net.*;
import ohos.telephony.*;
import tv.danmaku.ijk.utils.TextUtils;

import java.util.List;

/**
 * NetWork Utils
 */
public class NetWorkUtils {

    public static final String NETWORK_TYPE_WIFI = "wifi";
    public static final String NETWORK_TYPE_4G = "4g";
    public static final String NETWORK_TYPE_5G = "5g";
    public static final String NETWORK_TYPE_WAP = "wap";
    public static final String NETWORK_TYPE_UNKNOWN = "unknown";
    public static final String NETWORK_TYPE_DISCONNECT = "disconnect";

    /**
     * Get network type
     *
     * @param context
     * @return
     */
    public static int getNetworkType(Context context) {
        NetManager netManager = NetManager.getInstance(context);
        NetCapabilities netCapabilities = netManager.getNetCapabilities(netManager.getDefaultNet());
        int netWorkType;
        if (netCapabilities.hasBearer(NetCapabilities.BEARER_WIFI)) {
            netWorkType = NetCapabilities.BEARER_WIFI;
        } else if (netCapabilities.hasBearer(NetCapabilities.BEARER_CELLULAR)) {
            netWorkType = NetCapabilities.BEARER_CELLULAR;
        } else if (netCapabilities.hasBearer(NetCapabilities.BEARER_BLUETOOTH)) {
            netWorkType = NetCapabilities.BEARER_BLUETOOTH;
        } else if (netCapabilities.hasBearer(NetCapabilities.BEARER_ETHERNET)) {
            netWorkType = NetCapabilities.BEARER_ETHERNET;
        } else if (netCapabilities.hasBearer(NetCapabilities.BEARER_VPN)) {
            netWorkType = NetCapabilities.BEARER_VPN;
        } else if (netCapabilities.hasBearer(NetCapabilities.BEARER_WIFI_AWARE)) {
            netWorkType = NetCapabilities.BEARER_WIFI_AWARE;
        } else if (netCapabilities.hasBearer(NetCapabilities.BEARER_LOWPAN)) {
            netWorkType = NetCapabilities.BEARER_LOWPAN;
        } else {
            netWorkType = -1;
        }
        return netWorkType;
    }

    /**
     * Get network type name
     *
     * @param context
     * @return
     */
    public static String getNetworkTypeName(Context context) {

        NetManager netManager = NetManager.getInstance(context);
        NetCapabilities netCapabilities = netManager.getNetCapabilities(netManager.getDefaultNet());
        String type = NETWORK_TYPE_DISCONNECT;
        boolean isWifi = netCapabilities.hasBearer(NetCapabilities.BEARER_WIFI);
        boolean isMobile = netCapabilities.hasBearer(NetCapabilities.BEARER_CELLULAR);
        if (netCapabilities.hasCap(NetCapabilities.NET_CAPABILITY_VALIDATED)) {
            if (isWifi) {
                type = NETWORK_TYPE_WIFI;
            } else if (isMobile) {
                String host = netManager.getDefaultHttpProxy().getHost();
                type = TextUtils.isEmpty(host) ? (isFastMobileNetwork(context)
                        ? NETWORK_TYPE_5G : NETWORK_TYPE_4G)
                        : NETWORK_TYPE_WAP;

            } else {
                type = NETWORK_TYPE_UNKNOWN;
            }
        }
        return type;
    }

    /**
     * Whether is fast mobile network
     *
     * @param context
     * @return
     */
    private static boolean isFastMobileNetwork(Context context) {
        RadioInfoManager radioInfoManager = RadioInfoManager.getInstance(context);

        // 获取信号信息。
        List<SignalInformation> signalList = radioInfoManager.getSignalInfoList(0);

        // 检查信号信息列表大小。
        if (signalList.size() == 0) {
            return false;
        }
        // 依次遍历list获取当前驻网networkType对应的信号信息。
        LteSignalInformation lteSignal;
        for (SignalInformation signal : signalList) {
            switch (signal.getNetworkType()) {
                case TelephonyConstants.RADIO_TECHNOLOGY_1XRTT:
                    return false;
                case TelephonyConstants.RADIO_TECHNOLOGY_GSM:
                    return false;
                case TelephonyConstants.RADIO_TECHNOLOGY_EVDO:
                    return true;
                case TelephonyConstants.RADIO_TECHNOLOGY_HSPA:
                    return true;
                case TelephonyConstants.RADIO_TECHNOLOGY_HSPAP:
                    return true;
                case TelephonyConstants.RADIO_TECHNOLOGY_EHRPD:
                    return true;
                case TelephonyConstants.RADIO_TECHNOLOGY_NR:
                    return true;
                case TelephonyConstants.NETWORK_TYPE_UNKNOWN:
                    return false;
                default:
                    return false;
            }
        }
        return false;
    }

    /**
     * 网络是否可用
     *
     * @param context
     * @return
     */
    public static boolean isNetworkAvailable(Context context) {
        NetManager netManager = NetManager.getInstance(context);
        NetCapabilities netCapabilities = netManager.getNetCapabilities(netManager.getDefaultNet());
        // NetCapabilities.NET_CAPABILITY_VALIDATED表示连接了网络，并且可以上网
        return netCapabilities.hasCap(NetCapabilities.NET_CAPABILITY_VALIDATED);
    }

    /**
     * 判断MOBILE网络是否可用
     */
    public static boolean isMobileConnected(Context context) {
        if (context != null) {
            NetManager netManager = NetManager.getInstance(context);
            NetCapabilities netCapabilities
                    = netManager.getNetCapabilities(netManager.getDefaultNet());
            return netCapabilities.hasCap(NetCapabilities.NET_CAPABILITY_VALIDATED) &&
                    netCapabilities.hasBearer(NetCapabilities.BEARER_CELLULAR);
        }
        return false;
    }

    /**
     * 判断WIFI网络是否可用
     */
    public static boolean isWifiConnected(Context context) {
        if (context != null) {
            NetManager netManager = NetManager.getInstance(context);
            NetCapabilities netCapabilities
                    = netManager.getNetCapabilities(netManager.getDefaultNet());
            return netCapabilities.hasCap(NetCapabilities.NET_CAPABILITY_VALIDATED)
                    && netCapabilities.hasBearer(NetCapabilities.BEARER_WIFI)
                    || netCapabilities.hasBearer(NetCapabilities.BEARER_WIFI_AWARE);
        }
        return false;
    }
}
